<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Finance extends Model
{
    //
    protected $table = 'finance';
    protected $guarded = [];

    public function resume()
    {
        return $this->belongsTo('App\Models\Home\Resume','resume_id','id');
    }

    public function position()
    {
        return $this->belongsTo('App\Models\Home\Position','position_id','id');
    }

    public function client()
    {
        return $this->belongsTo('App\Models\Home\Client','client_id','id');
    }

    public function from()
    {
        return $this->belongsTo('App\Models\Home\Users','from_id','id');
    }

    public function assign_user()
    {
        return $this->belongsTo('App\Models\Home\Users','assign','id');
    }

    public function all_schedule()
    {
        return $this->hasMany('App\Models\Home\FinanceSchedule', 'finance_id', 'id');
    }

    public function getList($skip,$limit,$opt=array(),$in=array()){

        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $query = Finance::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }
        if (!empty($in) && count($in)==2){
            $query = $query->whereIn($in[0],$in[1]);
        }

        $query = $query->skip($skip)->limit($limit);

        $query = $query->with(['resume','position','client','from','assign_user']);

        $query = $query->with(['all_schedule']);

        $list = $query->get();

        foreach ($list as $k => $v){
            $list[$k]->entry_time = date('Y-m-d',strtotime($v->entry_time));
            $list[$k]->resume_name = $v->resume?$v->resume->name:'';
            $list[$k]->position_name = $v->position?$v->position->title:'';
            $list[$k]->client_name = $v->client?$v->client->chinese_name:'';
            $list[$k]->from_name = $v->from?$v->from->username:'';
            $from_type = $v->from?$v->from->type:0;
            switch ($from_type){
                case 1:
                    $list[$k]->from_type = '内部推荐 - ';
                    break;
                case 2:
                    $list[$k]->from_type = '兼职推荐 - ';
                    break;
                default:
                    $list[$k]->from_type = '自投';
            }

            $list[$k]->receipts_0 = 0;
            $list[$k]->receipts_1 = 0;

            foreach ($v->all_schedule as $item){
                if ($item->type == 1){
                    $list[$k]->receipts_1 += intval($item->receipts);
                }else{
                    $list[$k]->receipts_0 += intval($item->receipts);
                }

            }
            unset($list[$k]->all_schedule);
            $list[$k]->profit = intval($list[$k]->receipts_1-$list[$k]->receipts_0);

            $list[$k]->assign_name = $v->assign_user?$v->assign_user->username:'';
        }

        return $list;
    }

    public function getCount($opt=array()){

        $query = Finance::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        return $query->count();
    }

}
